package com.czmh.etc.onlinepubsys.controller.cmg;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.czmh.etc.common.core.utils.Sequence;
import com.czmh.etc.onlinepubsys.constants.cmg.BusinessType;
import com.czmh.etc.onlinepubsys.entity.mingchuang.ApplyInfo;
import com.czmh.etc.onlinepubsys.entity.mingchuang.CurrentContext;
import com.czmh.etc.onlinepubsys.entity.mingchuang.EtcOrder;
import com.czmh.etc.onlinepubsys.enums.cmg.ApiCodeEnum;
import com.czmh.etc.onlinepubsys.json.cmg.JSONResult;
import com.czmh.etc.onlinepubsys.model.EtcAfterSaleDevice;
import com.czmh.etc.onlinepubsys.model.EtcFallActivate;
import com.czmh.etc.onlinepubsys.model.EtcReplace;
import com.czmh.etc.onlinepubsys.request.cmg.*;
import com.czmh.etc.onlinepubsys.request.mingchuang.SubmitFallActivateRequest;
import com.czmh.etc.onlinepubsys.response.cmg.ApiResponseBase;
import com.czmh.etc.onlinepubsys.services.EtcAfterSaleDeviceService;
import com.czmh.etc.onlinepubsys.services.EtcFallActivateService;
import com.czmh.etc.onlinepubsys.services.EtcOrderService;
import com.czmh.etc.onlinepubsys.services.EtcReplaceService;
import com.czmh.etc.onlinepubsys.services.mingchuang.ICurrentContextService;
import com.czmh.etc.onlinepubsys.services.mingchuang.IEtcApplyService;
import com.czmh.etc.onlinepubsys.services.mingchuang.IObuService;
import com.czmh.etc.onlinepubsys.utils.RedisUtil;
import com.czmh.etc.onlinepubsys.utils.cmg.CmgHttpUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/{version}/api/cmg/ioapi/newActivate")
@Api(tags = "发行激活接口相关 - 招商局")
public class NewCmgActivateController {

    @Autowired
    private ICurrentContextService currentContextService;

    @Value("${newcmg.toAccessNo}")
    private String toAccessNo;

    @Value("${newcmg.authAccessNo}")
    private String authAccessNo;

    @PostMapping("/getToken")
    @ApiOperation("获取授权访问token接口")
    public JSONResult getToken(@RequestBody NewGetTokenRequest req) {
        JSONResult result = new JSONResult();
        log.info("newgetToken:{}", JSON.toJSONString(req));
        //为操作填充订单号信息
        CurrentContext currentContext = currentContextService.getByUserId(req.getUserId());
        Long time = System.currentTimeMillis();
        req.setBusinessFlowNo(toAccessNo + time);
        req.setTime(time);
        req.setToAccessNo(authAccessNo);
        ApiResponseBase r = CmgHttpUtil.newpost(BusinessType.NEW_AUTH_GET_TOKEN, req);
        if (r != null && r.getCode().equalsIgnoreCase(ApiCodeEnum.SUCCESS.getCode())) {
            if (r.bizContent != null) {
                result.setRspData(r.getBizContent());
                result.setMessage(r.getMsg());
                result.setCurrentTime(r.getReceiveTime());
                //手机请求结果数据不改变用户请求流程状态
                //currentContext.setState(4);
                //判断流程是否存在
                if (currentContext != null && StringUtils.isNotEmpty(currentContext.getOrderNo())) {
                    String jsonStr = currentContext.getJsonData();
                    JSONObject jsonObject = JSON.parseObject(jsonStr);
                    if (jsonObject == null) {
                        jsonObject = new JSONObject();
                    }
                    jsonObject.put("jsonData", r.getBizContent());
//                    jsonObject.put("getToken", r);
//                    JSONObject jsonObject2 = JSONObject.parseObject(jsonObject.getString("getToken"));
//                    String businessFlowNo = jsonObject2.getString("businessFlowNo");
                    currentContext.setJsonData(jsonObject.toJSONString());
//                    currentContext.setBusinessFlowNo(businessFlowNo);
                    currentContextService.update(currentContext);
                } else {
                    log.info("用户不在流程：", req.getUserId());
                }
            }
        } else {
            result.setCode(r != null ? r.getCode() : JSONResult.FAIL);
            result.setMessage(r.getMsg());
            result.setCurrentTime(r.getReceiveTime());
        }

        return result;
    }

    @PostMapping("/issueActive")
    @ApiOperation("预录入发行激活信息接口")
    public JSONResult issueActive(@RequestBody NewIssueActiveRequest req) {
        JSONResult result = new JSONResult();
        log.info("issueActive:{}", JSON.toJSONString(req));
        CurrentContext currentContext = currentContextService.getByUserId(req.getUserId());
        Long time = System.currentTimeMillis();
        req.setTime(time);
        req.setToAuthAccessNo(authAccessNo);
        ApiResponseBase r = CmgHttpUtil.newpost(BusinessType.ISSUEACTIVE_PREADD, req);
        if (r != null && r.getCode().equalsIgnoreCase(ApiCodeEnum.SUCCESS.getCode())) {
            result.setRspData(r.getCode());
            result.setMessage(r.getMsg());
            result.setCurrentTime(r.getReceiveTime());
        } else {
            result.setCode(r != null ? r.getCode() : JSONResult.FAIL);
            result.setMessage(r.getMsg());
            result.setCurrentTime(r.getReceiveTime());
        }
        return result;
    }
}

